package org.client.otdr.executor;

import java.util.Arrays;
import java.util.List;

import org.client.otdr.Executor;
import org.client.otdr.OTDRID;
import org.client.otdr.OtdrDomainService;
import org.openems.entity.ChoiceEntity;
import org.openems.entity.Entity;
import org.openems.entity.Key;

import framework.AbstractDomainService.ActiveAccessor;
import framework.EntityStoreListener;
import framework.webclient.RestSocket;
import framework.webclient.SystemProperty;

public class SetupExecutor implements Executor {

	private ActiveAccessor accessor;
	private List<Key> ignores;

	public SetupExecutor(ActiveAccessor accessor, RestSocket restSocket,
			SystemProperty systemProperty) {
		this.accessor = accessor;
		ignores = Arrays.asList(OTDRID.get().CONTROL);
	}

	@Override
	public void execute(Key key) {
		final OTDRID id = OTDRID.get();
		final ChoiceEntity e = (ChoiceEntity)accessor.get(id.CONTROL);
		if (e.getValueText().equals(OtdrDomainService.MEASUREMENT_START)) {
			EntityStoreListener listener = new EntityStoreListener() {
				@Override
				public void valueChanged(Entity entity) {
					accessor.removeEntityStoreListener(this);
					
					ChoiceEntity e2 = (ChoiceEntity)accessor.get(id.CONTROL);
					e2.setValueText(OtdrDomainService.MEASUREMENT_START);
					accessor.put(e2);
				}
			};
			accessor.addEntityStoreListener(listener, id.CONTROL);
			e.setValueText(OtdrDomainService.MEASUREMENT_STOP);
			accessor.put(e);
		}

	}

}
